<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}菜单`">
    <el-form
      ref="ruleFormRef"
      label-width="150px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="捐赠主体名称" prop="meta.name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写捐赠主体名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="捐赠主体联系方式" prop="meta.contact_information">
        <el-input v-model="drawerProps.row!.contact_information" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="商户订单号" prop="meta.out_trade_no">
        <el-input v-model="drawerProps.row!.out_trade_no" placeholder="请填写商户订单号" clearable></el-input>
      </el-form-item>
      <el-form-item label="微信预付单号" prop="meta.prepay_id">
        <el-input v-model="drawerProps.row!.prepay_id" placeholder="请填写微信预付单号" clearable></el-input>
      </el-form-item>
      <el-form-item label="付款状态">
        <el-radio-group v-model="drawerProps.row!.pay_status">
          <el-radio v-for="item in pay_status" :key="item.value" :label="item.label" :value="item.value">{{
            item.label
          }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="种植区域名称" prop="planting_area_name">
        <el-input v-model="drawerProps.row!.planting_area_name" placeholder="请填写种植区域名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="种植区域名称" prop="planting_area_name">
        <el-input v-model="drawerProps.row!.planting_area_name" placeholder="请填写种植区域名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="种植树种名称" prop="planting_tree_name">
        <el-input v-model="drawerProps.row!.planting_tree_name" placeholder="请填写种植树种名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="种植树种规格名称" prop="planting_tree_specification_name">
        <el-input
          v-model="drawerProps.row!.planting_tree_specification_name"
          placeholder="请填写种植树种规格名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="捐赠主体类型">
        <el-radio-group v-model="drawerProps.row!.type">
          <el-radio v-for="item in type" :key="item.value" :label="item.label" :value="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="捐赠金额" prop="donate_amount">
        <el-input v-model="drawerProps.row!.donate_amount" placeholder="请填写捐赠金额" clearable></el-input>
      </el-form-item>
      <el-form-item label="捐赠数量" prop="donate_number">
        <el-input v-model="drawerProps.row!.donate_number" placeholder="请填写捐赠数量" clearable></el-input>
      </el-form-item>
      <el-form-item label="捐赠总额" prop="donate_total">
        <el-input v-model="drawerProps.row!.donate_total" placeholder="请填写捐赠总额" clearable></el-input>
      </el-form-item>
      <el-form-item label="openid" prop="openid_mp">
        <el-input v-model="drawerProps.row!.openid_mp" placeholder="请填写openid" clearable></el-input>
      </el-form-item>
      <el-form-item label="付款时间" prop="pay_at">
        <el-input v-model="drawerProps.row!.pay_at" placeholder="请填写付款时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="捐赠时间" prop="donate_at">
        <el-input v-model="drawerProps.row!.donate_at" placeholder="请填写捐赠时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="created_at">
        <el-input v-model="drawerProps.row!.created_at" placeholder="请填写创建时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updated_at">
        <el-input v-model="drawerProps.row!.updated_at" placeholder="请填写更新时间" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="menuDrawer">
import { ref, reactive } from "vue";
import { pay_status, type } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { Tree } from "@/api/interface";
import { getMenuTreeAll } from "@/api/modules/menu";
import { useAuthStore } from "@/stores/modules/auth"; // 根据实际路径导入

const authStore = useAuthStore(); //仓库

const rules = reactive({
  name: [{ required: true, message: "请填写规格名称" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Tree.ResRecordList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 创建计算属性，将 pid 数组转换为单个值
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      console.log(drawerProps.value, "drawerProps.value");
      const row = drawerProps.value.row;
      await drawerProps.value.api!(row);
      ElMessage.success({ message: `${drawerProps.value.title}菜单成功！` });
      // drawerProps.value.getTableList!();
      const myMenuData = await getMenuTreeAll();
      authStore.setMenuArr(myMenuData);
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
